package GUI.lecturerGUI;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.color.CMMException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

import GUI.MainGUI;
import ReportHistogram.ReportHistogram;

import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JFormattedTextField;
import javax.swing.JTable;

import controllers.LecturerController;
import controllers.PrincipalController;

public class LecturerReportFile extends JPanel implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JButton bExit;
	private JButton genetatexam;
	private JButton btnMainMenu;
	private JButton btnChooseOtherExam;
	private JLabel lblNumberOfStudent;
	private JFormattedTextField FTxtFldNumberOfStudent;
	private JLabel lblExamAvg;
	private JFormattedTextField fTxtFldAvg;
	private JLabel lblExamCode;
	private JFormattedTextField fTxtFldExamCode;
	private JLabel lblMedian;
	private JFormattedTextField fTxtFldMedian;
	private JFrame histogramFrame;
	private LecturerController lecturerCtrl;
	private JButton btnHistogram;
	private int[] histogrades;
/**
 * the lecturer report file GUI
 * @param lecturerCtrl - the lecturer controller 
 */
	public LecturerReportFile(LecturerController lecturerCtrl) {

		super();
		this.lecturerCtrl = lecturerCtrl;
		initialize();

	}
	/**
	 * initializing the GUI
	 */
	private void initialize() {
		setBorder(new TitledBorder(null, "Lecturer LecturerReport file: ",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setBackground(SystemColor.activeCaption);
		setBounds(0, 0, MainGUI.setX, MainGUI.setY);
		setLayout(null);

		add(getBtnExit());
		add(GETbtnMainMenu());
		add(GETbtnChooseOtherExam());
		add(GETlblNumberOfStudent());
		add(GETFTxtFldNumberOfStudent());
		add(GETlblExamAvg());
		add(GETfTxtFldAvg());
		add(GETlblExamCode());
		add(GETfTxtFldExamCode());
		add(GETlblMedian());
		add(GETfTxtFldMedian());
		add(getBtnHistogram());
	}

	private JButton GETbtnMainMenu() {
		if (btnMainMenu == null) {
			btnMainMenu = new JButton("MAIN MENU");
			btnMainMenu.setFont(btnMainMenu.getFont().deriveFont(
					btnMainMenu.getFont().getStyle() | Font.BOLD));
			setBounds(0, 0, MainGUI.setX, MainGUI.setY);
			btnMainMenu.addActionListener(this);
		}
		return btnMainMenu;
	}

	private JButton GETbtnChooseOtherExam() {
		if (btnChooseOtherExam == null) {
			btnChooseOtherExam = new JButton("Back to exam selection");
			btnChooseOtherExam.setBounds(10, 449, 242, 40);
			btnChooseOtherExam.addActionListener(this);
		}
		return btnChooseOtherExam;
	}

	private JLabel GETlblNumberOfStudent() {
		if (lblNumberOfStudent == null) {
			lblNumberOfStudent = new JLabel(
					"The Number Of Students Who Committed The Exam");
			lblNumberOfStudent.setBackground(new Color(175, 238, 238));
			lblNumberOfStudent.setBounds(21, 77, 339, 14);
		}
		return lblNumberOfStudent;
	}

	private JFormattedTextField GETFTxtFldNumberOfStudent() {
		if (FTxtFldNumberOfStudent == null) {
			FTxtFldNumberOfStudent = new JFormattedTextField();
			lblNumberOfStudent.setLabelFor(FTxtFldNumberOfStudent);
			FTxtFldNumberOfStudent.setBackground(new Color(147, 112, 219));
			FTxtFldNumberOfStudent.setBounds(375, 74, 90, 20);
			FTxtFldNumberOfStudent.addActionListener(this);
		}
		return FTxtFldNumberOfStudent;
	}

	private JLabel GETlblExamAvg() {
		if (lblExamAvg == null) {
			lblExamAvg = new JLabel("Exam average:");
			lblExamAvg.setBounds(21, 121, 90, 14);
		}
		return lblExamAvg;
	}

	private JFormattedTextField GETfTxtFldAvg() {
		if (fTxtFldAvg == null) {
			fTxtFldAvg = new JFormattedTextField();
			fTxtFldAvg.setBackground(new Color(147, 112, 219));
			fTxtFldAvg.setBounds(375, 118, 90, 20);
			fTxtFldAvg.addActionListener(this);
		}
		return fTxtFldAvg;

	}

	private JLabel GETlblExamCode() {
		if (lblExamCode == null) {
			lblExamCode = new JLabel("Exam Code:");
			lblExamCode.setBounds(21, 34, 106, 14);
		}
		return lblExamCode;
	}

	private JFormattedTextField GETfTxtFldExamCode() {
		if (fTxtFldExamCode == null) {
			fTxtFldExamCode = new JFormattedTextField();
			fTxtFldExamCode.setBackground(new Color(147, 112, 219));
			fTxtFldExamCode.setBounds(375, 34, 90, 20);
			fTxtFldExamCode.addActionListener(this);
		}
		return fTxtFldExamCode;
	}

	private JLabel GETlblMedian() {
		if (lblMedian == null) {
			lblMedian = new JLabel("Median:");
			lblMedian.setBounds(23, 162, 46, 14);
		}
		return lblMedian;
	}

	private JFormattedTextField GETfTxtFldMedian() {
		if (fTxtFldMedian == null) {
			fTxtFldMedian = new JFormattedTextField();
			fTxtFldMedian.setBackground(new Color(147, 112, 219));
			fTxtFldMedian.setBounds(375, 159, 90, 20);
			fTxtFldMedian.addActionListener(this);

		}
		return fTxtFldMedian;
	}



	private JButton getBtnExit() {

		if (bExit == null) {
			bExit = new JButton("EXIT");
			bExit.setSize(new Dimension(75, 40));
			bExit.setLocation(415, 449);
			bExit.setFont(new Font("Microsoft New Tai Lue ", Font.BOLD, 12));
			bExit.setBorder(BorderFactory.createLineBorder(SystemColor.desktop));
			bExit.addActionListener(this);
		}
		return bExit;
	}

	@Override
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == genetatexam) {
			lecturerCtrl.handleLectutereGUI(LecturerController.REPORTFILE);
		}
		if (e.getSource() == bExit) {
			lecturerCtrl.handleLectutereGUI(LecturerController.EXIT);
		}
		if (e.getSource() == btnMainMenu) {
			lecturerCtrl.handleLectutereGUI(LecturerController.MAIN);

		}
		if (e.getSource() == btnChooseOtherExam) {
			
			lecturerCtrl.handleLectutereGUI(LecturerController.REPORT);
		}
		if (e.getSource() == btnHistogram) {
			String[] acolumnHeader={new String("0-10"),new String("10-20"),new String("20-30"),new String("30-40"),new String("40-50"),new String("50-60"),new String("60-70"),new String("70-80"),new String("80-90"),new String("90-100"),};
			int[] avalues={new Integer(histogrades[0]),new Integer(histogrades[1]),new Integer(histogrades[2]),new Integer(histogrades[3]),new Integer(histogrades[4]),new Integer(histogrades[5]),new Integer(histogrades[6]),new Integer(histogrades[7]),new Integer(histogrades[8]),new Integer(histogrades[9]),};
			
			int ahistogramHeight=1000;
			int amaxColumnWidth=1000;
			
			try {
				ReportHistogram h=new ReportHistogram(acolumnHeader, avalues, ahistogramHeight, amaxColumnWidth);
		
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}

	}

	/**
	 * set text field whit  the Correct variables
	 * @param examcode
	 * @param size
	 * @param avg
	 * @param madian
	 * @param counter
	 */
	
	public void settextfield(String examcode, int size, Float avg, float madian,int[]counter) {
		fTxtFldAvg.setText(Float.toString(avg));
		fTxtFldMedian.setText(Float.toString(madian));
		FTxtFldNumberOfStudent.setText(Integer.toString(size));
		fTxtFldExamCode.setText(examcode);
		fTxtFldAvg.setEditable(false);
		fTxtFldExamCode.setEditable(false);
		fTxtFldMedian.setEditable(false);
		FTxtFldNumberOfStudent.setEditable(false);
		
		histogrades=new int[10];
		histogrades.clone();
		for (int i=0 ;i<10;i++){
			histogrades[i]=	counter[i];
		}

	}

	private JButton getBtnHistogram() {
		if (btnHistogram == null) {
			btnHistogram = new JButton("histogram");
			btnHistogram.setBounds(21, 237, 89, 23);
			btnHistogram.addActionListener(this);
		}
		return btnHistogram;
	}
	/**
	 * 
	 * @param h -draw the lecturer histogram
	 * @throws InterruptedException
	 */
	public void dohistogram(ReportHistogram h) throws InterruptedException {
		histogramFrame=new JFrame();
	
		
		histogramFrame.setLocation(100,100);
		histogramFrame.setSize(500, 500);
		histogramFrame.setVisible(true);
		histogramFrame.setContentPane(h);
		
	}
		
	
}
